/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.animation;

import com.android1.amarena2d.engine.EngineObject;
import com.android1.amarena2d.texture.ManagedTexture;
import com.android1.amarena2d.texture.TextureFrame;

import java.util.ArrayList;

public class DefaultAnimationBuilder extends EngineObject {

    ArrayList<TextureFrame> textureFrames = new ArrayList<TextureFrame>(30);
    String name;
    float delay;

    DefaultAnimationBuilder() {
    }

    DefaultAnimationBuilder init(float delay) {
        this.delay = delay;
        textureFrames.clear();
        name = null;
        return this;
    }

    public DefaultAnimationBuilder(float delay) {
        this.delay = delay;
    }

    public DefaultAnimationBuilder addFrame(TextureFrame frame) {
        textureFrames.add(frame);
        return this;
    }

    public DefaultAnimationBuilder addFrame(ManagedTexture frame) {
        textureFrames.add(frame.getSharedTexturedMesh().getTextureFrame());
        return this;
    }

    public DefaultAnimationBuilder addFrame(String texturePath) {
        addFrame(engine.getTextureManager().getTexture(texturePath));
        return this;
    }

    public DefaultAnimationBuilder name(String name) {
        this.name = name;
        return this;
    }


    public Animation create() {
        if (textureFrames.size() == 0) {
            throw new IllegalStateException("Animations need at least one frame.");
        }
        Animation.Frame[] frames = new Animation.Frame[textureFrames.size()];
        for (int i = 0; i < textureFrames.size(); i++) {
            TextureFrame textureFrame = textureFrames.get(i);
            frames[i] = new Animation.FrameImpl(textureFrame);
        }
        return new SimpleAnimation(name, delay, frames);
    }
}
